package com.ruoyi.report_hntjg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.report_hntjg.domain.bo.ReportHntjgHntqdhtfbgBaDetailBo;
import com.ruoyi.report_hntjg.mapper.ReportHntjgHntqdhtfbgBaDetailMapper;
import com.ruoyi.report_hntjg.service.IReportHntjgHntqdhtfbgBaDetailService;
import com.ruoyi.report_hntjg.domain.ReportHntjgHntqdhtfbgBaDetail;
import com.ruoyi.report_hntjg.domain.vo.ReportHntjgHntqdhtfbgBaDetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 混凝土强度试验检测报告（回弹法）-详情Service业务层处理
 *
 * @author tll
 * @date 2024-08-07
 */
@RequiredArgsConstructor
@Service
public class ReportHntjgHntqdhtfbgBaDetailServiceImpl implements IReportHntjgHntqdhtfbgBaDetailService {

    private final ReportHntjgHntqdhtfbgBaDetailMapper baseMapper;

    /**
     * 查询混凝土强度试验检测报告（回弹法）-详情
     */
    @Override
    public ReportHntjgHntqdhtfbgBaDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询混凝土强度试验检测报告（回弹法）-详情列表
     */
    @Override
    public TableDataInfo<ReportHntjgHntqdhtfbgBaDetailVo> queryPageList(ReportHntjgHntqdhtfbgBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportHntjgHntqdhtfbgBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportHntjgHntqdhtfbgBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询混凝土强度试验检测报告（回弹法）-详情列表
     */
    @Override
    public List<ReportHntjgHntqdhtfbgBaDetailVo> queryList(ReportHntjgHntqdhtfbgBaDetailBo bo) {
        LambdaQueryWrapper<ReportHntjgHntqdhtfbgBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportHntjgHntqdhtfbgBaDetail> buildQueryWrapper(ReportHntjgHntqdhtfbgBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportHntjgHntqdhtfbgBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportHntjgHntqdhtfbgBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitbh()), ReportHntjgHntqdhtfbgBaDetail::getWeitbh, bo.getWeitbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShifbs()), ReportHntjgHntqdhtfbgBaDetail::getShifbs, bo.getShifbs());
        lqw.eq(StringUtils.isNotBlank(bo.getLingq()), ReportHntjgHntqdhtfbgBaDetail::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getCeqsn()), ReportHntjgHntqdhtfbgBaDetail::getCeqsn, bo.getCeqsn());
        lqw.eq(bo.getHuanszPjz() != null, ReportHntjgHntqdhtfbgBaDetail::getHuanszPjz, bo.getHuanszPjz());
        lqw.eq(bo.getHuanszBzc() != null, ReportHntjgHntqdhtfbgBaDetail::getHuanszBzc, bo.getHuanszBzc());
        lqw.eq(bo.getHuanszZxz() != null, ReportHntjgHntqdhtfbgBaDetail::getHuanszZxz, bo.getHuanszZxz());
        lqw.eq(bo.getTongqdTdz() != null, ReportHntjgHntqdhtfbgBaDetail::getTongqdTdz, bo.getTongqdTdz());
        lqw.eq(bo.getCeq1() != null, ReportHntjgHntqdhtfbgBaDetail::getCeq1, bo.getCeq1());
        lqw.eq(bo.getCeq2() != null, ReportHntjgHntqdhtfbgBaDetail::getCeq2, bo.getCeq2());
        lqw.eq(bo.getCeq3() != null, ReportHntjgHntqdhtfbgBaDetail::getCeq3, bo.getCeq3());
        lqw.eq(bo.getCeq4() != null, ReportHntjgHntqdhtfbgBaDetail::getCeq4, bo.getCeq4());
        lqw.eq(bo.getCeq5() != null, ReportHntjgHntqdhtfbgBaDetail::getCeq5, bo.getCeq5());
        lqw.eq(bo.getCeq6() != null, ReportHntjgHntqdhtfbgBaDetail::getCeq6, bo.getCeq6());
        lqw.eq(bo.getCeq7() != null, ReportHntjgHntqdhtfbgBaDetail::getCeq7, bo.getCeq7());
        lqw.eq(bo.getCeq8() != null, ReportHntjgHntqdhtfbgBaDetail::getCeq8, bo.getCeq8());
        lqw.eq(bo.getCeq9() != null, ReportHntjgHntqdhtfbgBaDetail::getCeq9, bo.getCeq9());
        lqw.eq(bo.getCeq10() != null, ReportHntjgHntqdhtfbgBaDetail::getCeq10, bo.getCeq10());
        return lqw;
    }

    /**
     * 新增混凝土强度试验检测报告（回弹法）-详情
     */
    @Override
    public Boolean insertByBo(ReportHntjgHntqdhtfbgBaDetailBo bo) {
        ReportHntjgHntqdhtfbgBaDetail add = BeanUtil.toBean(bo, ReportHntjgHntqdhtfbgBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改混凝土强度试验检测报告（回弹法）-详情
     */
    @Override
    public Boolean updateByBo(ReportHntjgHntqdhtfbgBaDetailBo bo) {
        ReportHntjgHntqdhtfbgBaDetail update = BeanUtil.toBean(bo, ReportHntjgHntqdhtfbgBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportHntjgHntqdhtfbgBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除混凝土强度试验检测报告（回弹法）-详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
